package system

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type Decimal_deviceService struct{}

// CreateDecimal_device 创建医疗设备记录
// Author [yourname](https://github.com/yourname)
func (decimal_deviceService *Decimal_deviceService) CreateDecimal_device(ctx context.Context, decimal_device *system.Decimal_device) (err error) {
	err = global.GVA_DB.Create(decimal_device).Error
	return err
}

// DeleteDecimal_device 删除医疗设备记录
// Author [yourname](https://github.com/yourname)
func (decimal_deviceService *Decimal_deviceService) DeleteDecimal_device(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&system.Decimal_device{}, "id = ?", ID).Error
	return err
}

// DeleteDecimal_deviceByIds 批量删除医疗设备记录
// Author [yourname](https://github.com/yourname)
func (decimal_deviceService *Decimal_deviceService) DeleteDecimal_deviceByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.Decimal_device{}, "id in ?", IDs).Error
	return err
}

// UpdateDecimal_device 更新医疗设备记录
// Author [yourname](https://github.com/yourname)
func (decimal_deviceService *Decimal_deviceService) UpdateDecimal_device(ctx context.Context, decimal_device system.Decimal_device) (err error) {
	err = global.GVA_DB.Model(&system.Decimal_device{}).Where("id = ?", decimal_device.ID).Updates(&decimal_device).Error
	return err
}

// GetDecimal_device 根据ID获取医疗设备记录
// Author [yourname](https://github.com/yourname)
func (decimal_deviceService *Decimal_deviceService) GetDecimal_device(ctx context.Context, ID string) (decimal_device system.Decimal_device, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&decimal_device).Error
	return
}

// GetDecimal_deviceInfoList 分页获取医疗设备记录
// Author [yourname](https://github.com/yourname)
func (decimal_deviceService *Decimal_deviceService) GetDecimal_deviceInfoList(ctx context.Context, info systemReq.Decimal_deviceSearch) (list []system.Decimal_device, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.Decimal_device{})
	var decimal_devices []system.Decimal_device
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&decimal_devices).Error
	return decimal_devices, total, err
}
func (decimal_deviceService *Decimal_deviceService) GetDecimal_devicePublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

// UploadImage 图片上传
// Author [yourname](https://github.com/yourname)
func (decimal_deviceService *Decimal_deviceService) UploadImage(ctx context.Context, deviceID string, imageURL string) error {
	// 查询设备记录
	var device system.Decimal_device
	if err := global.GVA_DB.Where("id = ?", deviceID).First(&device).Error; err != nil {
		return err
	}

	// 更新图片URL
	device.Images_url = &imageURL
	return global.GVA_DB.Save(&device).Error
}
